/**
	Copyright (c) 2009 Ozazo Pvt Ltd, INDIA

	Permission is hereby granted, free of charge, to any person or organization 
	obtaining a copy of this software and associated  documentation  files (the 
	"Software"), to deal in the Software without restriction, including without 
	limitation the rights to use, copy, modify, merge, publish, distribute, sub
	license, and/or sell copies of the Software, and to permit persons  to whom 
	the Software is furnished to do so, subject to the following conditions:
	
	The above copyright notice and  this permission notice shall be included in 
	all copies or substantial portions of the Software.
	
	THE SOFTWARE IS PROVIDED "AS IS",  WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING  BUT NOT  LIMITED  TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR  COPYRIGHT  HOLDERS  BE  LIABLE  FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY,  WHETHER  IN  AN  ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
	FROM,  OUT  OF  OR  IN  CONNECTION  WITH  THE  SOFTWARE OR THE USE OR OTHER 
	DEALINGS IN THE SOFTWARE.
 */
package com.ozazo.wave.robots.fw.engines;

import com.google.wave.api.Annotation;
import com.google.wave.api.Range;
import com.google.wave.api.TextView;

/**
 * @author Araf Karsh Hamid
 * @version 1.0
 * @seriel 20091121102030
 *
 */
public abstract class AbstractRobotEngine implements RobotEngineInterface {

	/**
	 * Set Annotation after checking if one exists in the same place. If yes then 
	 * Do NOTHING.
	 * 
	 * Feature request 192 in Google Issue tracker.
	 * 
	 * @param txtView
	 * @param range
	 * @param name
	 * @param value
	 */
	
	public void setAnnotation(TextView txtView, Range range, String name, String value) {
		if( (txtView == null) || (range == null) ) {
			return;
		}
 		// If this annotation is already present the DO NOTHING.
		// Note that we allow the existing annotation to be bigger than the one we're
		// creating, because in that case, setting the new annotation won't do anything useful.
		/**
		for (Annotation annotation : txtView.getAnnotations(range, name)) {
		      if (annotation.getValue().equals(value) 
		    		  && annotation.getRange().getStart() <= range.getStart() 
		    		  && range.getEnd() <= annotation.getRange().getEnd()) {
			        return;
		    	  
		      }
		}
		*/
		// txtView.setAnnotation(range, range.getStart()+":"+range.getEnd()+":"+name, value);
		txtView.setAnnotation(range, name, value);
	}
	
	/**
	 * Searches for a given text in the Text View and returns a range based on the text size
	 * and its position in the original text in the Text View.
	 * 
	 * @param TextView txtView
	 * @param String _txt
	 * @return Range
	 */
	
	public Range getRange(final TextView txtView, String _txt) {
		int sRange = txtView.getText().indexOf(_txt);
		int eRange = sRange + _txt.length();
		Range txtRange = new Range(sRange, eRange);
		return txtRange;
	}	
}
